<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    destroy-on-close
    :close-on-click-modal="false"
    @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter="dataFormSubmit()" label-width="80px">
      <el-form-item label="主键id" prop="id" v-if="dataForm.id">
          <el-input v-model="dataForm.id" placeholder="主键id" disabled></el-input>
      </el-form-item>
      <!-- <el-form-item label="用户id" prop="userId">
          <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
      </el-form-item> -->
      <el-form-item label="用户名" prop="userName">
          <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="用户电话" prop="userPhone">
          <el-input v-model="dataForm.userPhone" placeholder="用户电话"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="dataForm.carNumber" placeholder="车牌号"></el-input>
      </el-form-item>
      <el-form-item label="行驶里程" prop="travelMile">
          <el-input v-model="dataForm.travelMile" placeholder="行驶里程"> 
            <template #append>M</template>
          </el-input>
      </el-form-item>
      <el-form-item label="加油金额" prop="amount">
          <el-input v-model="dataForm.amount" placeholder="加油金额">
            <template #append>元</template>
          </el-input>
      </el-form-item>
      <el-form-item label="加油站" prop="stationName">
          <el-input v-model="dataForm.stationName" placeholder="点击选择加油站">
            <template #append>
              <el-icon><Location /></el-icon>
            </template>
          </el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
          <el-input v-model="dataForm.address" placeholder="详细地址" disabled></el-input>
      </el-form-item>
      <!-- <el-form-item label="经度" prop="longitude">
          <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
          <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
      </el-form-item> -->
      <el-form-item label="现场照片" prop="siteImage">
          <el-upload
              :action="uploadURL"
              name="file"
              accept=".jpg,.jpeg,.png,.JPG,.JPEG,.PNG"
              :limit="limitCount"
              list-type="picture-card"
              :class="{hide:siteImageHideUpload}"
              :on-preview="siteImagePreview"
              :on-remove="siteImageRemove"
              :on-change="siteImageChange"
              :http-request="siteImageUpload">
              <el-icon><Plus /></el-icon>
            <template #tip>
              <div class="el-upload__tip">
                只能上传jpg/jpeg/png格式图片, 且不超过5M.
              </div>  
            </template>
          </el-upload>
          <el-dialog v-model="siteImagePreviewVisible">
              <img style="width: 100%; height: auto;" :src="siteImagePreviewPath" alt="Preview Image">
          </el-dialog>
      </el-form-item>
      <el-form-item label="收据照片" prop="receiptImage">
          <el-input v-model="dataForm.receiptImage" placeholder="收据照片"></el-input>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
          <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
          <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
          <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
          <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/smart/oilrecord'
    import {listAllCarNumber} from '@/api/smart/oilcar'
    import {uploadFile} from '@/api/smart/oilfile'
    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,

        uploadURL: '/smart/oilfile/upload',
        limitCount: 1,
        
        siteImagePreviewPath: '',
        siteImagePreviewVisible: false,
        siteImageHideUpload: false,
        siteImageFile: '',
        siteImageFileList: '',
        siteImage: [],

        receiptImagePreviewPath: '',
        receiptImagePreviewVisible: false,
        receiptImageHideUpload: false,
        receiptImageFile: '',
        receiptImageFileList: '',
        receiptImage: [],

        dataForm: {
          id: '',
          userId: '',
          userName: '',
          carNumber: '',
          travelMile: '',
          amount: '',
          stationName: '',
          address: '',
          longitude: '',
          latitude: '',
          // siteImage: '',
          receiptImage: '',
          createBy: '',
          updateBy: '',
          createTime: '',
          updateTime: '',
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],

          userPhone: [
            { required: true, message: '用户电话不能为空', trigger: 'blur' }
          ],

          carNumber: [
            { required: true, message: '车牌号不能为空', trigger: 'blur' }
          ],

          travelMile: [
            { required: true, message: '行驶里程不能为空', trigger: 'blur' }
          ],

          amount: [
            { required: true, message: '加油金额不能为空', trigger: 'blur' }
          ],

          stationName: [
            { required: true, message: '加油站不能为空', trigger: 'blur' }
          ],

          // address: [
          //   { required: true, message: '详细地址不能为空', trigger: 'blur' }
          // ],

          // longitude: [
          //   { required: true, message: '经度不能为空', trigger: 'blur' }
          // ],

          // latitude: [
          //   { required: true, message: '纬度不能为空', trigger: 'blur' }
          // ],

          siteImage: [
            { required: true, message: '现场照片不能为空', trigger: 'change' }
          ],

          receiptImage: [
            { required: true, message: '收据照片不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            // this.$refs['dataForm'].resetFields()
            this.dataForm.id = ''
            this.dataForm.createBy = ''
            this.dataForm.updateBy = ''
            this.dataForm.createTime = ''
            this.dataForm.updateTime = ''
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },

      // beforeUpload(file){
      //   if(file.size/1024/1024 > 5){
      //     ElMessage.error('文件大小超出上限')
      //   }
      //   return false
      // },
      // 处理图片预览效果
      siteImagePreview(file) {
        this.siteImagePreviewPath = file.url;
        this.siteImagePreviewVisible = true
      },
      siteImageRemove(file, fileList) {
        if(fileList.length == 0){
            this.dataRule.siteImage = [{ required: true, message: '请上传现场照片', trigger: 'change'}];
        }
        this.siteImageHideUpload = fileList.length >= this.limitCount;
      },
      siteImageChange(file, fileList) {
        this.siteImageFile = file;
        this.siteImageFileList = fileList;
        if(fileList.length == 1) {
            let {siteImage, ...data} = this.dataRule;
            this.dataRule = data;
        }
        this.$refs.dataForm.clearValidate('siteImage');
        this.siteImageHideUpload = fileList.length >= this.limitCount;
      },
      siteImageUpload(params) {
        let param = new FormData();
        param.append("file", params.file);
        uploadFile(param).then(res => {
            this.siteImage = [];
            this.siteImage.push(res.data.data)
        })
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //查询所有车牌号
      listAllCarNumber(){
        listAllCarNumber().then(response => {
          this.carNumbers = response.data.data
        }).catch(() => {

        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }

    }
  }
</script>
<style>
  .hide .el-upload--picture-card {
    display: none;
  }
</style>